package com.thirtydays.frame.util

import android.bluetooth.BluetoothGatt
import com.blankj.utilcode.util.LogUtils
import com.thirtydays.frame.constant.NOTIFY_UUID
import com.thirtydays.frame.constant.SERVICE_UUID
import com.thirtydays.frame.constant.WRITE4_UUID
import com.thirtydays.frame.util.crc.CRC16
import com.thirtydays.frame.util.ext.hex2ByteArray
import com.xm.bluetoothkotlin.BluetoothManager
import com.xm.bluetoothkotlin.connect.BleBluetooth
import com.xm.bluetoothkotlin.connect.ConnectConfigData
import com.xm.bluetoothkotlin.connect.back.BleNotifyCallbackImp
import com.xm.bluetoothkotlin.connect.back.BleWriteCallback
import com.xm.bluetoothkotlin.connect.back.BluetoothConnectCallback
import com.xm.bluetoothkotlin.device.BluetoothDeviceData

/**
 * @创建者：小垚
 * @时间：2023/3/8  17:06
 * @描述：
 */
object BluetoothHelp {
    private val writeCallback by lazy {
        object : BleWriteCallback() {
            override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray) {
                LogUtils.e("onWriteSuccess")
                addListener()
            }

            override fun onWriteFailure(error: String) {
                LogUtils.e("onWriteFailure")
            }

        }
    }


    private val dataCallback by lazy {
        object : BluetoothConnectCallback {
            override fun onStartConnect() {
                LogUtils.e("开始连接")
            }

            override fun onConnectFail(device: BleBluetooth, message: String) {
                LogUtils.e("连接失败")
            }

            override fun onConnectSuccess(device: BleBluetooth, gatt: BluetoothGatt, status: Int) {
                LogUtils.e("连接成功")
                LogUtils.e(writeShake(device.device.address))
                BluetoothManager.write(device.device, writeShake(device.device.address), writeCallback)
            }

            override fun onDisConnected(isActiveDisConnected: Boolean, device: BleBluetooth, gatt: BluetoothGatt, status: Int) {
                LogUtils.e("连接中。。。")
            }
        }
    }

    private var deviceData: BluetoothDeviceData? = null
    private val configData by lazy {
        ConnectConfigData().apply {
            service_uuid = SERVICE_UUID
            write_characteristic_uuid = WRITE4_UUID
            notify_characteristic_uuid = NOTIFY_UUID
        }
    }

    fun connect(deviceMac: String) {
        deviceData = BluetoothManager.connect(deviceMac, configData, dataCallback)

    }

    fun addListener() {
        deviceData?.run {
            BluetoothManager.notify(this,configData,false, object : BleNotifyCallbackImp() {
                override fun onNotifySuccess() {
                    LogUtils.e("通知成功")
                }

                override fun onNotifyFailure(error: String) {
                    LogUtils.e("通知失败")
                }

                override fun onCharacteristicChanged(data: ByteArray, uuid: String) {
                    LogUtils.e(uuid)
                }
            })
        }
    }

    //正式握手协议
    fun writeShake(deviceMac: String): ByteArray {
        val bytesToHex = CRC16().crc16BytesWithCCITT_False(deviceMac.hex2ByteArray())
        return byteArrayOf(
            0x5A,
            0xF1.toByte(),
            0x00,
            0x00,
            0x00,
            bytesToHex[1],
            bytesToHex[0],
            0xA5.toByte()
        )
    }
}